package com.xm.socket;

import com.xm.util.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

@Slf4j
public class Client {
    public static void main(String[] args) {
        startOneClient(7070);
//        startOneClient(7071);
//        startFileClient(7072);
//        startChatClient(7073, "小明");
    }

    /**
     * 开启一个客户端
     */
    public static void startOneClient(int port) {
        try (Socket socket = new Socket("localhost", port)) {
            log.info("Client[{}] started，try connect to server...", port);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //发送消息
            out.println("Hello from client");

            //读取服务器响应
            String msg = in.readLine();
            log.info("Received from server：{}", msg);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 开启一个文件客户端
     */
    public static void startFileClient(int port) {
        try (Socket socket = new Socket("localhost", port)) {
            log.info("FileClient[{}] started，try connect to server...", port);

            String upload = FileUtil.getResourcePathUtf8("socket/client.txt");
            try (OutputStream out = socket.getOutputStream();
                 FileInputStream fileIn = new FileInputStream(new File(upload))) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = fileIn.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            }
            log.info("File sent successfully. upload={}", upload);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 开启一个聊天客户端
     */
    public static void startChatClient(int port, String name) {
        try (Socket socket = new Socket("localhost", port)) {
            log.info("ChatClient[{}] started，try connect to server...", port);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            new Thread(() -> {
                try {
                    String message;
                    while ((message = in.readLine()) != null) {
                        log.info("Received：{}", message);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }).start();

            Scanner scanner = new Scanner(System.in);
            String msg = "";
            while (!"exit".equalsIgnoreCase(msg)) {
                msg = scanner.nextLine();
                out.println(name + "：" + msg);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
